package com.factory.test.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.cloud.idcard.IdCardReader;
import com.cloud.idcard.ReadIdCardListener;
import com.cloud.idcard.UsbIdCardReader;
import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityDkIdcardBinding;
import com.factory.test.util.SoundManager;
import com.yecontech.library_card.IDCard;

/**
 * 德科二代证
 */
public class DKidCardActivity extends BaseTestActivity {

    private Context context;
    private ActivityDkIdcardBinding binding;
    private IdCardReader idCardReader;
    private SoundManager soundManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;

        binding = DataBindingUtil.setContentView(this, R.layout.activity_dk_idcard);
        binding.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.textView.setText("");
            }
        });
        soundManager = new SoundManager(context);

        idCardReader = new UsbIdCardReader();
        idCardReader.init(context);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // todo 开始读卡
        idCardReader.start(new ReadIdCardListener() {
            @Override
            public void onProgress(int progress) {

            }

            @Override
            public void onReadCarSn(String cardSn) {
                soundManager.playBeep();
                appendCard(cardSn);
            }

            @Override
            public void onReadIdCard(IDCard idCard) {
                soundManager.playBeep();
                appendCard(idCard.toString());
                appendIdCardImage(idCard.getPhoto());
            }

            @Override
            public void onError(RuntimeException exception) {
                appendCard(exception.getMessage());
            }
        });
    }

    private Handler handler = new Handler();
    private void appendCard(final String cardNo){
        handler.post(new Runnable() {
            @Override
            public void run() {
                if(binding.textView.length() > 10240){
                    binding.textView.setText("");
                }
                binding.textView.append(cardNo);
                binding.textView.append("\r\n");
                binding.scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

    private void appendIdCardImage(final Bitmap bitmap){
        if(bitmap == null){
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                ImageSpan span = new ImageSpan(context, bitmap);
                SpannableString spanStr = new SpannableString(" ");
                spanStr.setSpan(span, 0, spanStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                binding.textView.append(spanStr);
                binding.textView.append("\n\n");
                binding.scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        idCardReader.stop();        //todo 停止读卡
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        idCardReader.close();       //todo 一定要释放, 否则下次不可以再打开
        soundManager.release();
    }
}
